package com.otitan.la.forest.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.auth.framework.basepro.common.AuthCommon;
import com.otitan.la.forest.system.constant.DicEnum;
import com.otitan.la.forest.system.dto.LogDTO;
import com.otitan.la.forest.system.entity.LogEntity;
import com.otitan.la.forest.system.mapper.LogMapper;
import com.otitan.la.forest.system.service.DicService;
import com.otitan.la.forest.system.service.LogService;
import com.otitan.la.forest.system.util.IpUtil;
import com.otitan.la.forest.system.vo.LogVO;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;

@Slf4j
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity> implements LogService {

    @Autowired
    private DicService dicService;
    @Autowired
    private AuthCommon authCommon;

    @Override
    public boolean setSystemLogger(LogEntity logUser, String tokenId) {
        JSONObject user = null;
        if (tokenId == null ){
            user = authCommon.getLoginUserInfo();
        } else {
            user = authCommon.getLoginUserInfo(tokenId);
        }
        if (user != null){
            logUser.setUserId(user.get("id").toString());
            logUser.setIp(IpUtil.getIpAddr());
            logUser.setUserName(user.get("userName").toString());
            logUser.setRealName(user.get("realName").toString());
            return this.save(logUser);
        }
        return false;
    }

    @Override
    public DataResult add(LogEntity log) {
        boolean isSuccess = setSystemLogger(log, null);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除日志表】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(LogEntity log, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        boolean isSuccess = this.updateById(log);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(long page, long size, LogDTO log) {
        Page<LogVO> pagePlug = new Page<>(page, size);
        Page<LogVO> voPage=pagePlug.setRecords(this.baseMapper.findPageLogData(log, pagePlug));
        List<LogVO> logVOList=voPage.getRecords();
        if(CollUtil.isNotEmpty(logVOList)){
            logVOList.forEach(logVO->{
                String actionName=dicService.getDicName(DicEnum.LOG_OPERATE_TYPE.getValue(),logVO.getAction());
                logVO.setActionName(actionName);
            });
        }
        return DataResult.success(voPage);
    }

    @Override
    public DataResult findOne(String id) {
        return DataResult.success(this.getById(id));
    }
}